﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Diploma.Utils;
using EmailSender;

namespace Diploma.Models.Utils
{
    public static class EmailUtil
    {
        private static readonly SmtpSender _emailSender;

        static EmailUtil()
        {
            _emailSender = new SmtpSender(ConstantsUtil.SMTP_HOST);
        }

        public static void Send(User sender, User recepient, string message, string subject)
        {
            Send(sender, recepient, message, subject, null);
        }

        public static void Send(User sender, User recepient, string message, string subject, string attachmentFileName)
        {
            Send(sender, recepient, message, subject, attachmentFileName);
        }

        public static void Send(User sender, IEnumerable<User> recepients, string message, string subject)
        {
            Send(sender, recepients, message, subject, null);
        }

        public static void Send(User sender, IEnumerable<User> recepients, string message, string subject,
                                string attachmentFilePath)
        {
            string messageBody = message;
            var sb = new StringBuilder();

            foreach (User recepient in recepients)
            {
                sb.Append(recepient.Email);
                sb.Append(",");
            }
            if (sb.Length != 0)
                sb[sb.Length - 1] = ' ';

            var attachmentsList = new MessageAttachmentList();
            string attachmentFileLink = null;

            if (attachmentFilePath != null)
            {
                var attachment = new MessageAttachment(attachmentFilePath);
                attachmentsList.Add(attachment);
                string fileName = Path.GetFileName(attachmentFilePath);
                string downloadLink = string.Format(@"{0}\{1}\{2}", PathUtil.WebSiteBaseUrl,
                                                    ConstantsUtil.GetPublicFilesDirectoryPath(PathType.AppDepending),
                                                    fileName);
                attachmentFileLink = String.Format("<a href='{0}'>{1}</a>", downloadLink, fileName);
            }

            var emailMessage = new EmailSender.Message
                                   {
                                       Body =
                                           String.Format("<h2>{0}:</h2>{1}</br>{2}", sender.FullName, message,
                                                         attachmentFileLink),
                                       Subject = subject,
                                       To = "diplomauser@diploma.com",
                                       From = "diploma@diploma.com",
                                       Attachments = attachmentsList,
                                       Bcc = sb.ToString(),
                                       Format = Format.Html,
                                       Encoding = Encoding.UTF8,
                                   };

            _emailSender.Send(emailMessage);
        }
    }
}